package com.lagou.web.servlet;

import com.lagou.utils.UUIDUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;


@WebServlet("/upload")
public class UploadFile extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       try{
        //创建磁盘文件工厂类
        DiskFileItemFactory factory = new DiskFileItemFactory();
        //调用核心类
        ServletFileUpload upload = new ServletFileUpload(factory);

        //设置编码格式
           upload.setHeaderEncoding("utf-8");
           //判断是否文件上传 是TRUE 否FALSE
        boolean multipartContent = upload.isMultipartContent(req);
        if (multipartContent) {
            //获取到表单集合

            List<FileItem> list = upload.parseRequest(req);

            if(list!=null){
                for(FileItem item:list){
                    //判断每一项是普通表单还是文件上传表单
                    boolean formField = item.isFormField();

                    if(formField){
                        //普通表单
                        //获取表单属性
                        String fieldName = item.getFieldName();
                        //获取表单值
                        String  value = item.getString("utf-8");
                        System.out.println(fieldName+"="+value);


                    }else{

                        //文件上传表单
                        String name = item.getName();//获取上传文件的名字
                        String newName= UUIDUtils.getUUID()+"_"+name;
                        //读取上传文件
                        InputStream in = item.getInputStream();
                         //输出文件
                        //获取路径
                        String realPath = this.getServletContext().getRealPath("/");
                        System.out.println(realPath);

                        //D:\软件\apache-tomcat-8.5.79-windows-x64\apache-tomcat-8.5.79\webapps\test_war\

                        String path = realPath.substring(0, realPath.indexOf("test_war"));

                        //要有一个空目录
                        FileOutputStream out = new FileOutputStream(path+"/public/"+newName);
                        //copy到路径地址下
                        IOUtils.copy(in,out);
                        //关闭
                        in.close();
                        out.close();
                    }
                }
            }

        }
        }catch (FileUploadException e){
           e.printStackTrace();
       }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
